﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace 模型绑定.Models
{
    //一个模型有很多自定义校验和判断可以实现IValidatableObject接口
    //[Bind(nameof(EmployeeName),nameof(Address))]  //设置值模型绑定EmployeeName和Address
    public class Employee : IValidatableObject
    {
        public int Id { get; set; }

        [Display(Name = "姓名")]
        [Required(ErrorMessage = "姓名不能为空")]
        public string EmployeeName { get; set; }

        [Display(Name = "详细地址")]
        [BindRequired]
        //Required：表示当前字段是必填选项，当提交的表单缺少该值就引发验证错误。括号里的AllowEmptyStrings是： 获取或设置一个值，该值指示是否允许空字符串。单值为false的时候表示当前字段的值不能为空
        //[Required(AllowEmptyStrings = false, ErrorMessage = "详细地址必填")]
        public string Address { get; set; }


        [Display(Name = "出生日期")]
        [Required(ErrorMessage = "出生日期不能为空")]
        [DataType(DataType.Date, ErrorMessage = "出生日期格式不正确")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] //格式化显示的效果
        public DateTime? BirthDate { get; set; }

        [Display(Name = "手机号码")]
        [Required(ErrorMessage = "手机号码不能为空")]
        [DataType(DataType.PhoneNumber, ErrorMessage = "手机号码格式不正确")]
        [RegularExpression(@"^[1-9]\d{10}$")]
        public string Phone { get; set; }


        [Display(Name = "邮箱")]
        [Required(ErrorMessage = "邮箱不能为空")]
        [DataType(DataType.EmailAddress, ErrorMessage = "邮箱格式不正确")]
        public string Email { get; set; }


        //DisplayFormat:用来处理属性的各种格式化选项。当属性包含空值时，可以提供可选的显示文本，也可以为包含标记的属性关闭HTML编码，还可以为运行时指定一个应用于属性值的格式化字符串。
        [Display(Name = "薪资")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")] //格式化显示的效果
        [Required(ErrorMessage = "{0}不能为空")]
        [RegularExpression(@"^(\d+)(\.\d{0,2})?$", ErrorMessage = "{0}格式不正确!")]
        //[Range(1,100000,ErrorMessage ="{0}必须在{1}-{2}之间")]
        //[SalaryCheck]
        public decimal Salary { get; set; }

        [Display(Name ="奖金")]
        //[BindRequired] //表单元素提交的参数名称必须要有Bonus,参数值是否为空并不关心
        public decimal Bonus { get; set; }

        [BindNever] //针对表单请求会忽略对参数的绑定，但是通过[FromBody] 标识不会忽略对参数的绑定
        public bool IsAdmin { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!string.IsNullOrWhiteSpace(Email) && Email.Contains("admin"))
            {
                yield return new ValidationResult("email error",new string[] { "Email" }); //第二个参数是要验证的属性
            }
        }
    }

    ///// <summary>
    ///// 自定义验证(只能服务器验证)
    ///// </summary>
    //public class SalaryCheck : ValidationAttribute
    //{
    //    /// <summary>
    //    /// IsValid
    //    /// </summary>
    //    /// <param name="value">传递过来要验证的值</param>
    //    /// <param name="validationContext">通过validationContext可以拿到当前模型实体的实例</param>
    //    /// <returns></returns>
    //    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    //    {
    //        //获取实体对象的实例
    //        var model = validationContext.ObjectInstance as Employee;

    //        if (model is null)
    //        {
    //            throw new ArgumentNullException(nameof(model));
    //        }

    //        if (model.Salary > 100000)
    //        {
    //            return new ValidationResult("薪资不能超过10w");
    //        }

    //        return ValidationResult.Success;
    //    }
    //}



}
